//
//  MainTabBarVC.m
//  lehuo
//
//  Created by ios开发 on 15/11/2.
//  Copyright © 2015年 paai. All rights reserved.
//

#import "MainTabBarVC.h"
#import "CZTabBar.h"
#import "FFNavigationController.h"
#import "SMHomeVC.h"
#import "SMPictureVC.h"
#import "SMFindVC.h"
#import "SMMineVC.h"

@interface MainTabBarVC () <CZTabBarDelegate>

@property (nonatomic, strong) NSMutableArray *items;

@property (nonatomic, weak) SMHomeVC *homeVC;

@property (nonatomic, weak) SMPictureVC *pictureVC;

@property (nonatomic, weak) UIViewController *publishVC;

@property (nonatomic, weak) SMFindVC *discoverVC;

@property (nonatomic, weak) SMMineVC *mineVC;

@end

@implementation MainTabBarVC

- (NSMutableArray *)items
{
    if (_items == nil) {

        _items = [NSMutableArray array];
        
    }
    return _items;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 添加所有子控制器
    [self setUpAllChildViewController];
    
    // 自定义tabBar
    [self setUpTabBar];
    
}

#pragma mark - 设置tabBar
- (void)setUpTabBar
{
    // 自定义tabBar
    CZTabBar *tabBar = [[CZTabBar alloc] initWithFrame:self.tabBar.frame];
    tabBar.backgroundColor = [UIColor whiteColor];
    
    // 设置代理
    tabBar.delegate = self;
    
    // 给tabBar传递tabBarItem模型
    tabBar.items = self.items;
    
    // 添加自定义tabBar
    [self.view addSubview:tabBar];
    
    // 移除系统的tabBar
    [self.tabBar removeFromSuperview];
}


#pragma mark - 当点击tabBar上的按钮调用
- (void)tabBar:(CZTabBar *)tabBar didClickButton:(NSInteger)index
{
    if (index == 0 && self.selectedIndex == index) { // 点击首页，刷新
        //[_homeVC refresh];
    }
    
    self.selectedIndex = index;
}

// 点击加号按钮的时候调用
- (void)tabBarDidClickPlusButton:(CZTabBar *)tabBar
{
    // 创建发布控制器
    NSLog(@"点击了 ➕");
    //UIViewController *publish = [[UIViewController alloc] init];
    //FFNavigationController *nav = [[FFNavigationController alloc] initWithRootViewController:publish];
    
    //[self presentViewController:nav animated:YES completion:nil];
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
}


#pragma mark - 添加所有的子控制器
- (void)setUpAllChildViewController
{
    // 首页
    SMHomeVC *home = [[SMHomeVC alloc] init];

    [self setUpOneChildViewController:home image:[UIImage imageNamed:@"tabbar_home"] selectedImage:nil title:@"首页"];
    _homeVC = home;
    
    // 图片
    SMPictureVC *picture = [[SMPictureVC alloc] init];
//    picture.view.backgroundColor = [UIColor blueColor];
    
    [self setUpOneChildViewController:picture image:[UIImage imageNamed:@"tabbar_picture"] selectedImage:nil title:@"图集"];
    _pictureVC = picture;
    
    
    // 发现
    SMFindVC *discover = [[SMFindVC alloc] init];
//    discover.view.backgroundColor = [UIColor greenColor];
    
    [self setUpOneChildViewController:discover image:[UIImage imageNamed:@"tabbar_discover"] selectedImage:nil title:@"发现"];
    _discoverVC = discover;
    
    // 我
    SMMineVC *mine = [[SMMineVC alloc] init];
//    mine.view.backgroundColor = [UIColor yellowColor];
    
    [self setUpOneChildViewController:mine image:[UIImage imageNamed:@"tabbar_mine"] selectedImage:nil title:@"我的"];
    _mineVC = mine;
    
}
// navigationItem决定导航条上的内容
// 导航条上的内容由栈顶控制器的navigationItem决定

#pragma mark - 添加一个子控制器
- (void)setUpOneChildViewController:(UIViewController *)vc image:(UIImage *)image selectedImage:(UIImage *)selectedImage title:(NSString *)title
{
    vc.title = title;
    vc.tabBarItem.image = image;
    vc.tabBarItem.selectedImage = selectedImage;

    // 保存tabBarItem模型到数组
    [self.items addObject:vc.tabBarItem];
    
    FFNavigationController *nav = [[FFNavigationController alloc] initWithRootViewController:vc];
    
    [self addChildViewController:nav];
}


@end

